#!/bin/sh

#pre_umount () {
#  [ -f /lib/systemd/system/automounter-nfs.service ] && systemctl stop automounter-nfs.service
#  [ -f /lib/systemd/system/automounter-smb.service ] && systemctl stop automounter-smb.service
#}

echo 0 > /sys/class/graphics/fbcon/cursor_blink

[ -n "$LOGFILE" ] || LOGFILE="/dev/null"
/usr/lib/xbmc/xbmc.bin $@
EXIT_CODE=$?

case "$EXIT_CODE" in
  64) # 64=powerdown
    echo "xbmc exited with EXIT_CODE=$EXIT_CODE at $(date). Shutting down..." >>$LOGFILE
#    pre_umount
    systemctl poweroff
    exit 0
  ;;
  66) # 66=reboot
    echo "xbmc exited with EXIT_CODE=$EXIT_CODE at $(date). Rebooting..." >>$LOGFILE
#    pre_umount
    systemctl reboot
    exit 0
  ;;
  *) # 0=quit, 65=restart-app; let systemd restart the service
    echo "xbmc exited with EXIT_CODE=$EXIT_CODE at $(date). Returning -1 to systemd..." >>$LOGFILE
    echo 1 > /sys/class/graphics/fbcon/cursor_blink
    exit 1
esac
